package cn.cas.mango.service;

import cn.cas.mango.util.RequestUtil.BuildRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.LinkedHashMap;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class CountService {

  @Autowired
  private ConfigService config;
  @Autowired
  private BuildRequest buildRequest;

  public LinkedHashMap<String, Integer> getDayCountMap(String start, String end) {
    String params = "{\n"
        + "    \"size\": 0,\n"
        + "    \"query\": {\n"
        + "        \"bool\": {\n"
        + "            \"filter\": [\n"
        + "                {\n"
        + "                    \"range\": {\n"
        + "                        \"news_publicdate\": {\n"
        + "                            \"gte\": \"" + start + "\",\n"
        + "                            \"lte\": \"" + end + "\"\n"
        + "                        }\n"
        + "                    }\n"
        + "                }\n"
        + "            ]\n"
        + "        }\n"
        + "    },\n"
        + "    \"_source\": {\n"
        + "        \"includes\": [\n"
        + "            \"news_title_zh\",\n"
        + "            \"news_author\",\n"
        + "            \"news_publictime\",\n"
        + "            \"news_publicdate\",\n"
        + "            \"news_website\",\n"
        + "            \"news_website_type\",\n"
        + "            \"news_content_zh\",\n"
        + "            \"id\",\n"
        + "            \"news_url\",\n"
        + "            \"news_type\",\n"
        + "            \"news_media_name\"\n"
        + "        ]\n"
        + "    },\n"
        + "    \"from\": 0,\n"
        + "    \"aggregations\": {\n"
        + "        \"count-day\": {\n"
        + "            \"date_histogram\": {\n"
        + "                \"field\": \"news_publicdate\",\n"
        + "                \"format\": \"yyyy-MM-dd\",\n"
        + "                \"min_doc_count\": 0,\n"
        + "                \"calendar_interval\": \"day\",\n"
        + "                \"order\": [\n"
        + "                    {\n"
        + "                        \"_key\": \"asc\"\n"
        + "                    }\n"
        + "                ]\n"
        + "            }\n"
        + "        }\n"
        + "    },\n"
        + "    \"track_total_hits\": 1000000000\n"
        + "}";
    JSONObject request = JSONObject.parseObject(params);
    RestTemplateBuilder builder = new RestTemplateBuilder();
    RestTemplate restTemplate = builder.basicAuthentication(config.getUsername(), config.getPassword()).build();
    String url = "http://" + config.getHost() + ":" + config.getPort() + "/" + buildRequest.partitionIndex(config.getIndex(), start, end) + "/_search";
    ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, request,
        JSONObject.class);
    JSONObject responseJson = Objects.requireNonNull(responseEntity.getBody())
        .getJSONObject("aggregations");
    JSONArray countDayArray = responseJson.getJSONObject("count-day").getJSONArray("buckets");
    LinkedHashMap<String, Integer> dayTotalMap = new LinkedHashMap<>();
    for (Object row : countDayArray) {
      JSONObject rowJson = (JSONObject) row;
      String day = rowJson.getString("key_as_string");
      Integer count = rowJson.getInteger("doc_count");
      dayTotalMap.put(day, count);
    }
    return dayTotalMap;
  }

}
